﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Collections.Specialized;
using UMC.Data;

namespace UMC.Web
{
    /// <summary>
    /// 人脸核验对话框
    /// </summary>
    public class UIFaceDialog : UIDialog
    {
        public UIFaceDialog(String name, String no, string nonce = null)
        {
            if (String.IsNullOrEmpty(nonce))
            {
                this.Config.Put("Face", new WebMeta().Put("Name", name, "NO", no));
            }
            else
            {
                this.Config.Put("Face", new WebMeta().Put("Name", name, "NO", no).Put("Nonce", nonce));

                var bytes = System.Buffers.ArrayPool<byte>.Shared.Rent(8 + UTF32Encoding.UTF8.GetByteCount(name) + UTF32Encoding.UTF8.GetByteCount(nonce));
                var idcode = Utility.IDCard(no);
                BitConverter.TryWriteBytes(bytes, idcode);
                var size = 8;
                size += UTF7Encoding.UTF8.GetBytes(name.AsSpan(), bytes.AsSpan(size, bytes.Length - size));
                var pid = new Guid(System.Security.Cryptography.MD5.HashData(bytes.AsSpan(0, size)));
                this.Config.Put("IdKey", Utility.Guid(pid));
                this.Config.Put("Secret", Utility.ToBase64String(bytes.AsSpan(0, size)));
                size += UTF7Encoding.UTF8.GetBytes(nonce.AsSpan(), bytes.AsSpan(size, bytes.Length - size));
                this.Config.Put("Order", Utility.ToBase64String(bytes.AsSpan(0, size)));
            }
        }
        /// <summary>
        /// 对话框类型
        /// </summary>
        protected override string DialogType
        {
            get { return "Face"; }
        }
        /// <summary>
        /// 开启声音
        /// </summary>
        public bool IsVoice
        {

            get
            {
                return this.Config.ContainsKey("IsVoice");
            }
            set
            {
                if (value)
                {
                    this.Config.Put("IsVoice", true);
                }
                else
                {

                    this.Config.Remove("IsVoice");
                }
            }
        }
    }
}
